$(document).ready(function(){

  var socket = new io.connect("/");

  socket.on("connect", function(){
    var user = $("#username").val();
    socket.emit('sign in', user);
  }); 

  socket.on('presense', function(user){
    var pre = "pre_" + user.name;
    $("#" + pre).text(user.name);
  });

  socket.on('del presense', function(name){
    var pre = "pre_" + name;
    $("#" + pre).html("");
  });

  socket.on('msg push', function(rows){
    val = rows.val;
    val = val.replace(/\r\n/g, '\n');
    val = val.replace(/\r/g, '\n');
    val = val.replace(/\n/g, '<br />');
    var msg = "<div id=\"msgbox\"" + " class=\"" + rows.name + "\">" +
           rows.date + "     " + rows.name + "<br><br>" +
           val + "</div>";
    $("#box").append(msg);

  });

  socket.on('del msg', function(){
    $("#box").html("");
  });

  $("#submit").click(function(){
    var user = $("#username").val();
    var msg = $("#msg").val();
    socket.emit('msg send', msg, user);
    $("#msg").text('');
  });
});
